package com.mindspore.federatedime.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.mindspore.federatedime.R;
import com.mindspore.federatedime.bean.EmojiBean;

import java.util.ArrayList;
import java.util.List;

public class EmojiAdapter extends RecyclerView.Adapter {

    private List<EmojiBean> emojiData = new ArrayList<>();
    private Context context;

    public EmojiAdapter(Context context, List<EmojiBean> emojiData) {
        this.context = context;
        this.emojiData = emojiData;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new GifHolder(View.inflate(parent.getContext(), R.layout.adapter_emoji, null));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Glide.with(context)
                .load(emojiData.get(position).getValue())
                .centerCrop()
                .placeholder(R.color.white)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(((GifHolder) holder).item_gif);
        ((GifHolder) holder).itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return emojiData.size();
    }

    class GifHolder extends RecyclerView.ViewHolder {

        private ImageView item_gif;

        public GifHolder(@NonNull View itemView) {
            super(itemView);
            item_gif = itemView.findViewById(R.id.item_gif);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
